package com.manes.agents;

/**
 *
 * @author Administrator
 */
import java.awt.BorderLayout;
import java.awt.Dimension;

import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.TimeConstrainedEvaluator;
import org.matheclipse.symja.plot.PlotWindow;
//import org.matheclipse.symja.EvalPanel.CalcThread;

import com.manes.gui.UIAgentGUI;
import  com.manes.behaviours.*;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.manes.util.ParametricPlotWindow;
import com.manes.util.Plot3DWindow;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UIAgent extends GuiAgent {
	transient protected UIAgentGUI gui; // this is the gui
	public static final int EXIT = 1000;
	public static final int SEND_EVENT = 1001;
	public static final int REC_EVENT = 1002;
	public static final int PLOT_EVENT = 1003;
	public static final int HELP_EVENT = 1004;
	public static final int PLOT3D_EVENT = 1005;
    public static final int PARAMPLOT_EVENT = 1006;
    public static final int METH_EVENT = 1007;
	
    // Recomandation window
	private JTextField jacTxtFld;
	private JTextField dimTxtFld;
	private JTextField formTxtFld;
	private JTextField linTxtFld;
	
	//method creation window
	private JTextField ruleTxtFld;
	private JTextField ruleDimTxtFld;
	private JTextField ruleformTxtFld;
	private JTextField rulejacTxtFld;
	private JTextField rulelinTxtFld;
	private JTextField solTxtFld;
	
	
	public static JFrame frame = null;
	public static EvalEngine EVAL_ENGINE = new EvalEngine();

	private ACLMessage lastReceivedMsg;

	private MessageTemplate template = MessageTemplate.and(
			MessageTemplate.MatchPerformative(ACLMessage.INFORM), // QUERY_IF to
																	// INFORM
			MessageTemplate.MatchOntology("mnsOnto")); // presence to mnsOnto

	@Override
	protected void setup() {
		// creates and shows the GUI
		gui = new UIAgentGUI(this);
		gui.setVisible(true);
		gui.setDimension(500, 500);
		// gui.setBounds(null);

		addBehaviour(new CyclicBehaviour(this) {
			public void action() {
				ACLMessage msg = myAgent.receive(template);
				if (msg != null) {
					setReceivedMsg(msg);
					lastReceivedMsg = msg;
				} else {
					block();
				}
			}
		});
	}

	@Override
	protected void onGuiEvent(GuiEvent ev) {
		switch (ev.getType()) {
		case EXIT:
			gui.dispose();
			gui = null;
			doDelete();
			break;
		case SEND_EVENT:
			doSend();
			break;
		case REC_EVENT:
			//doRec();
			createRec();
			break;
		case METH_EVENT:
			createMet();
			break;
		case PLOT_EVENT:
			JDialog window = new PlotWindow(frame);
			window.pack();
			window.setVisible(true);
			break;
		case HELP_EVENT:
			break;
		case PLOT3D_EVENT:
			JDialog window3D = new Plot3DWindow(frame);
			window3D.pack();
			window3D.setVisible(true);
			break;
		case PARAMPLOT_EVENT:
			JDialog parWin = new ParametricPlotWindow(frame);
			parWin.pack();
			parWin.setVisible(true);
			break;

		}
	}

	private void createMet() {
		// TODO Auto-generated method stub
		JFrame metGui = new JFrame();
		metGui.setTitle("New Method Creation");
		metGui.setSize(500, 400);
		
		JPanel defRuleMsgPanel = new JPanel();
		JLabel ruleLabel = new JLabel("Enter Rule Name:");
		ruleTxtFld = new JTextField(30);
		defRuleMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		defRuleMsgPanel.add(ruleLabel);
		defRuleMsgPanel.add(ruleTxtFld);
		
		JPanel ruleDimMsgPanel = new JPanel();
		JLabel ruleDimLabel = new JLabel("Enter RuleDim:     ");
		ruleDimTxtFld = new JTextField(30);
		ruleDimMsgPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		ruleDimMsgPanel.add(ruleDimLabel);
		ruleDimMsgPanel.add(ruleDimTxtFld);

		JPanel ruleJacobiMsgPanel = new JPanel();
		JLabel ruleJacobiLabel = new JLabel("Enter RuleJac:     ");
		rulejacTxtFld = new JTextField(30);
		ruleJacobiMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		ruleJacobiMsgPanel.add(ruleJacobiLabel);
		ruleJacobiMsgPanel.add(rulejacTxtFld);
		
		JPanel ruleFormMsgPanel = new JPanel();
		JLabel ruleFormLabel = new JLabel("Enter RuleForm:  ");
		ruleformTxtFld = new JTextField(30);
		//recTxtFld.setPreferredSize(new Dimension());
		ruleFormMsgPanel.add(ruleFormLabel);
		ruleFormMsgPanel.add(ruleformTxtFld);
		
		JPanel ruleLinMsgPanel = new JPanel();
		JLabel ruleLinFormLabel = new JLabel("Enter RuleLin:     ");
		rulelinTxtFld = new JTextField(30);
		ruleLinMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		ruleLinMsgPanel.add(ruleLinFormLabel);
		ruleLinMsgPanel.add(rulelinTxtFld);
		
		JPanel ruleSolMsgPanel = new JPanel();
		FlowLayout flowLayout1 = (FlowLayout) ruleSolMsgPanel.getLayout();
		flowLayout1.setAlignment(FlowLayout.LEFT);
		metGui.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		metGui.getContentPane().add(defRuleMsgPanel);
		metGui.getContentPane().add(ruleDimMsgPanel);
		metGui.getContentPane().add(ruleJacobiMsgPanel);
		metGui.getContentPane().add(ruleFormMsgPanel);
		metGui.getContentPane().add(ruleLinMsgPanel);
		metGui.getContentPane().add(ruleSolMsgPanel);

		JLabel ruleLinLabel = new JLabel("Enter RuleSol:     ");
		ruleSolMsgPanel.add(ruleLinLabel);
		solTxtFld = new JTextField(30);
		ruleSolMsgPanel.add(solTxtFld);
		
		JButton btnMethod = new JButton("Enter");
		btnMethod.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				doMet();
			}
		});
		btnMethod.setVerticalAlignment(SwingConstants.BOTTOM);
		metGui.getContentPane().add(btnMethod);
		ruleJacobiMsgPanel.setVisible(true);
		ruleDimMsgPanel.setVisible(true);
		ruleFormLabel.setVisible(true);
		//sysDimMsgPanel.setLayout(new BorderLayout());
		
		metGui.setVisible(true);
	}

	protected void doMet() {
		// TODO Auto-generated method stub
		if(lastReceivedMsg !=null){
			String defRuleMsg = ruleTxtFld.getText();
			String ruleDimMsg = ruleDimTxtFld.getText();
			String ruleJacMsg = rulejacTxtFld.getText();
			String ruleFormMsg = ruleformTxtFld.getText();
			String ruleLinMsg = rulelinTxtFld.getText();
			String ruleSolMsg = solTxtFld.getText();
			
			// create ACLMessage for method creation and set the content from text fields
			
			String defRuleJess = "(defrule "+defRuleMsg+"(dimensiunea_sistemului { dimens "+ruleDimMsg+" })(jacobianul_sistemului { jacobian "+ruleJacMsg+" })(forma_sistemului {forma "+ruleFormMsg+" })(grad_linearitate_al_sistemului { grad_lin "+ruleLinMsg+ " }) " +
											"=> (assert(solutia_sistemului( solutie "+ruleSolMsg+" ))))";
			
			ACLMessage sendRule = new ACLMessage(ACLMessage.INFORM);
			sendRule.addReceiver(new AID("jness", AID.ISLOCALNAME));
			sendRule.setConversationId("sendRule");
			sendRule.setContent(defRuleJess);
			
			send(sendRule);
		/*	ACLMessage defRuleSend = new ACLMessage(ACLMessage.INFORM);
			defRuleSend.addReceiver(new AID("jness", AID.ISLOCALNAME));
			defRuleSend.setConversationId("defRule");
			defRuleSend.setContent(defRuleMsg);
			
			ACLMessage defJacobiSend = new ACLMessage(ACLMessage.INFORM);
			defJacobiSend.addReceiver(new AID("jness", AID.ISLOCALNAME));
			defJacobiSend.setConversationId("ruleJacobi");
			defJacobiSend.setContent(ruleJacMsg);
			
			ACLMessage defDimSend = new ACLMessage(ACLMessage.INFORM);
			defDimSend.addReceiver(new AID("jness", AID.ISLOCALNAME));
			defDimSend.setConversationId("ruleDim");
			defDimSend.setContent(ruleDimMsg);
			
			ACLMessage defFormSend = new ACLMessage(ACLMessage.INFORM);
			defFormSend.addReceiver(new AID("jness", AID.ISLOCALNAME));
			defFormSend.setConversationId("ruleForm");
			defFormSend.setContent(ruleFormMsg);
			
			ACLMessage defLinSend = new ACLMessage(ACLMessage.INFORM);
			defLinSend.addReceiver(new AID("jness", AID.ISLOCALNAME));
			defLinSend.setConversationId("ruleLin");
			defLinSend.setContent(ruleLinMsg);
			
			ACLMessage ruleSol = new ACLMessage(ACLMessage.INFORM);
			ruleSol.addReceiver(new AID("jness", AID.ISLOCALNAME));
			ruleSol.setConversationId("ruleSol");
			ruleSol.setContent(ruleSolMsg);
			
			send(defRuleSend);
			send(defJacobiSend);
			send(defDimSend);
			send(defFormSend);
			send(defLinSend);
			send(ruleSol); */
			
			
		}
		
	}

	private void doRec() {
		// TODO Auto-generated method stub
		if (lastReceivedMsg != null) {
			String jacobiMsg = jacTxtFld.getText();
            String dimMsg = dimTxtFld.getText();
            String formMsg = formTxtFld.getText();
            String linMsg = linTxtFld.getText();
            
            //create ACLMessages for recomandation and set the content from text fields
            
			ACLMessage jacobiSend = new ACLMessage(ACLMessage.INFORM);
			jacobiSend.addReceiver(new AID("jness", AID.ISLOCALNAME));
			jacobiSend.setConversationId("jacobi");
			//msgSend.setLanguage("English");
			//msgSend.setOntology("mnsOnto");
			jacobiSend.setContent(jacobiMsg);
			
			ACLMessage dimSend = new ACLMessage(ACLMessage.INFORM);
			dimSend.addReceiver(new AID("jness",AID.ISLOCALNAME));
			dimSend.setConversationId("sysDim");
			dimSend.setContent(dimMsg);
			
			ACLMessage formSend = new ACLMessage(ACLMessage.INFORM);
			formSend.addReceiver(new AID("jness",AID.ISLOCALNAME));
			formSend.setConversationId("sysForm");
			formSend.setContent(formMsg);
			
			ACLMessage linSend = new ACLMessage(ACLMessage.INFORM);
			linSend.addReceiver(new AID("jness",AID.ISLOCALNAME));
			linSend.setConversationId("sysLin");
			linSend.setContent(linMsg);
			
			//assert facts into jess
			//assertJacobian(jacobiSend);
			
			send(jacobiSend);
			send(dimSend);
			send(formSend);
			send(linSend);
		}

	}

	private void doSend() {
		if (lastReceivedMsg != null ) {
			String textToSend = gui.getTextToSend();

			ACLMessage reply = lastReceivedMsg.createReply();
			reply.setPerformative(ACLMessage.INFORM);
			reply.setContent(textToSend);

			send(reply);
		} else {
			String textToSend = gui.getTextToSend();

			ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
			msg.addReceiver(new AID("mathAgent", AID.ISLOCALNAME));
			msg.setLanguage("English");
			msg.setOntology("mnsOnto");
			msg.setContent(textToSend);
		}

	}

	// creates input pane for jess recomandation system
	/**
	 * @wbp.parser.entryPoint
	 */
	private void createRec() {
		JFrame recgui = new JFrame();
		recgui.setTitle("Method Recomandation");
		recgui.setSize(500, 400);

		
		JPanel jacobiMsgPanel = new JPanel();
		JLabel sendLabel = new JLabel("Enter Jacobi:      ");
		jacTxtFld = new JTextField(30);
		jacobiMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		jacobiMsgPanel.add(sendLabel);
		jacobiMsgPanel.add(jacTxtFld);

		JPanel sysDimMsgPanel = new JPanel();
		JLabel sysDimLabel = new JLabel("Enter SysDim:    ");
		dimTxtFld = new JTextField(30);
		sysDimMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		sysDimMsgPanel.add(sysDimLabel);
		sysDimMsgPanel.add(dimTxtFld);
		
		JPanel sysFormMsgPanel = new JPanel();
		JLabel sysFormLabel = new JLabel("Enter SysForm:  ");
		formTxtFld = new JTextField(30);
		sysFormMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//recTxtFld.setPreferredSize(new Dimension());
		sysFormMsgPanel.add(sysFormLabel);
		sysFormMsgPanel.add(formTxtFld);
		
		JPanel sysLinMsgPanel = new JPanel();
		recgui.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		recgui.getContentPane().add(jacobiMsgPanel);
		recgui.getContentPane().add(sysDimMsgPanel);
		recgui.getContentPane().add(sysFormMsgPanel);
		recgui.getContentPane().add(sysLinMsgPanel);
		sysLinMsgPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		JLabel sysLinLabel = new JLabel("Enter SysLin:     ");
		sysLinMsgPanel.add(sysLinLabel);
		linTxtFld = new JTextField(30);
		sysLinMsgPanel.add(linTxtFld);
		
		JButton btnRecomand = new JButton("Recomand");
		btnRecomand.setVerticalAlignment(SwingConstants.BOTTOM);
		btnRecomand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				doRec();
			}
		});
		recgui.getContentPane().add(btnRecomand);
		jacobiMsgPanel.setVisible(true);
		sysDimMsgPanel.setVisible(true);
		//sysDimMsgPanel.setLayout(new BorderLayout());

		 recgui.setVisible(true);
	}

	private void setReceivedMsg(ACLMessage msg) {
		String recieveContent = msg.getContent();
		// String receivedData = msg.getContent() + " " +
		// msg.getSender().getName();

		gui.setReceivedMsg(recieveContent);

		/*
		 * if(recieveContent.equals("x^2+3")){ gui.setReceivedMsg(receivedData);
		 * } else { gui.setReceivedMsg("Fail"); ACLMessage fail =
		 * lastReceivedMsg.createReply();
		 * fail.setPerformative(ACLMessage.NOT_UNDERSTOOD);
		 * fail.setContent("resend");
		 * 
		 * send(fail); }
		 */

	}
	


}
